<template>
  <el-container class="home-Container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="fish" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!-- 左侧边栏 -->
      <el-aside :width="flag ? '64px' : '200px'">
        <div class="btn-toggle" @click="toggleCollapse">|||</div>

        <!--  :collapse-transition="false"取消动画效果 -->
        <!-- router 启动路由切换功能，其中index为需跳转的路径 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#3498db"
          :collapse="flag"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="inconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconArr[Math.floor(Math.random() * 10) % 4]"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧边栏 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      //左侧边栏数据
      menuList: [],
      //incons
      inconsObj: {
        125: 'el-icon-user',
        103: 'el-icon-s-promotion',
        101: 'el-icon-menu',
        102: 'el-icon-sunny',
        145: 'el-icon-connection',
      },
      iconArr: [
        'el-icon-right',
        'el-icon-bottom',
        'el-icon-top',
        'el-icon-back',
      ],
      flag: false,
      //当前点击的菜单
      activePath: '',
    }
  },
  created() {
    this.getMenuList()
    //刷新状态下
    this.activePath = window.sessionStorage.getItem('activePath')
    // console.log("h");
  },
  methods: {
    logout() {
      //清除token
      window.sessionStorage.clear()
      //跳转至登录页面
      this.$router.push('/login')
    },
    //获取所有菜单
    async getMenuList() {
      let { data: res } = await this.$http.get('menus')

      if (res.meta.status !== 200) {
        return this.$message.error('home请求错误')
      }
      // console.log(res);
      this.menuList = res.data
      // console.log(this.menuList);
    },
    //实现菜单折叠功能
    toggleCollapse() {
      this.flag = !this.flag
    },
    //保存链接的激活状态
    saveNavState(activePath) {
      //activePath为传递的哈希值
      //存储哈希值
      window.sessionStorage.setItem('activePath', activePath)

      //不刷新状态下
      this.activePath = activePath
    },
  },
}
</script>

<style scoped>
.home-Container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-header > div {
  display: flex;
  align-items: center;
}
.el-header > span {
  margin-left: 15px;
}
.el-header > div > img {
  width: 50px;
  height: 50px;
}
.el-aside {
  background-color: #333744;
}
.el-menu {
  /* 去掉右侧边框线 */
  border-right: none;
}
.el-main {
  background-color: #eaedf1;
}
.btn-toggle {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 34px;
  text-align: center;
  color: white;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>